'use strict';
const Service = require('egg').Service;
const puppeteer = require('puppeteer');
const { PDFDocument } = require('pdf-lib');
const fs = require('fs');

class Pdf2 extends Service{
	async create(data){
        const ctx = this.ctx;

        const browser = await puppeteer.launch({args: ['--disable-setuid-sandbox', '--no-sandbox']});
        const page = await browser.newPage();
        const yymm = data.date.split('-')[0] + '-' + data.date.split('-')[1];
        const online_url = data.url;

        let taskArr = [];

        //id作品主推
        if(data.part == 1 || data.part == 'all'){
            taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/home/'+ data.date);
            const res00 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=yxsjs',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm}))
                }
            })
            if(res00.data.code == '0'){
                for(let i=0;i<res00.data.data.length;i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/designer/'+i)
                }
            }
            const res10 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=ztcp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'0',pxfl:'0'}))
                }
            })
            if(res10.data.code == '0'){
                for(let i=0;i<res10.data.data.length;i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/works/main/'+i)
                }
            }
        }
        
        //id作品入围
        if(data.part == 2 || data.part == 'all'){
            const res20 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=ztcp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'0',pxfl:'1'}))
                }
            })
            if(res20.data.code == '0'){
                for(let i=0;i<Math.ceil(res20.data.data.length/2);i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/works/list/'+i)
                }
            }
        }

        //id作品-其他
        if(data.part == 3 || data.part == 'all'){
            const res30 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=qtzp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'0'}))
                }
            })
            if(res30.data.code == '0' && res30.data.data.length){
                for(let i=0;i<Math.ceil(res30.data.data.length/8);i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/works/other/'+i)
                }
            }else{
                return ctx.helper.fail('异常或者无数据');
            }
        }

        if(data.part == 4 || data.part == 'all'){
            // 创新表达主推
            const res40 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=ztcp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'1',pxfl:'0'}))
                }
            })
            if(res40.data.code == '0'){
                for(let i=0;i<res40.data.data.length;i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/innovate/main/'+i)
                }
            }

            // 创新表达入围
            const res50 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=ztcp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'1',pxfl:'1'}))
                }
            })
            if(res50.data.code == '0'){
                for(let i=0;i<Math.ceil(res50.data.data.length/2);i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/innovate/list/'+i)
                }
            }

            const res60 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=qtzp',{
                dataType: 'json',
                method: 'POST',
                data:{
                    data:encodeURI(JSON.stringify({yf:yymm,zplb:'1'}))
                }
            })
            if(res60.data.code == '0'){
                for(let i=0;i<Math.ceil(res60.data.data.length/8);i++){
                    taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/innovate/other/'+i)
                }
            }
        }

        // // 设计资讯
        // const res50 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=zxdt',{
        //     dataType: 'json',
        //     method: 'POST',
        //     data:{
        //         data:encodeURI(JSON.stringify({yf:yymm,fl:'0'}))
        //     }
        // })
        // if(res50.data.code == '0'){
        //     for(let i=0;i<Math.ceil(res50.data.data.splitPDF.length/2);i++){
        //         taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/info/'+i)
        //     }
        // }

        // // 部门动态
        // const res60 = await this.ctx.curl(online_url+'/workflow/solex/yf/iddzzz/iddzzzGetData.jsp?method=zxdt',{
        //     dataType: 'json',
        //     method: 'POST',
        //     data:{
        //         data:encodeURI(JSON.stringify({yf:yymm,fl:'1'}))
        //     }
        // })
        // if(res60.data.code == '0'){
        //     for(let i=0;i<Math.ceil(res60.data.data.splitPDF.length/2);i++){
        //         taskArr.push(online_url+'/workflow/solex/yf/iddzzz/index.html#/department/'+i)
        //     }
        // }

        const pdfDoc = await PDFDocument.create();

        for (let i = 0; i < taskArr.length; i++) {
            await page.goto(taskArr[i], {waitUntil: 'domcontentloaded'}).catch(err => console.log(err));
            await page.waitFor(2000);

            let temp_buffer = await page.pdf({
                // path: './app/public/pdf/example.pdf',
                width:'1600px',
                height:'900px',
                printBackground: true,  //打印背景图和背景色
                '-webkit-print-color-adjust': 'exact',
            });

            let temp_doc = await PDFDocument.load(temp_buffer)
            let [tempPage] = await pdfDoc.copyPages(temp_doc, [0])
            pdfDoc.addPage(tempPage);
        }

        try {
            let fileName = ctx.helper.generateSecret(10);
            // 合成pdf
            fs.writeFileSync('./app/public/pdf/'+ data.date + fileName +'.pdf', await pdfDoc.save());
            return ctx.helper.success({url:'/public/pdf/'+ data.date + fileName +'.pdf'},"成功");

        } catch (e) {
            return ctx.helper.fail('异常');
        } finally {
            await browser.close();
        }
	}
}
module.exports = Pdf2;